<template>
  <div>
    <van-nav-bar title="登录" class="page-nav-bar">
      <van-icon slot="left" name="cross" @click="$router.back()"/>
    </van-nav-bar>
    <van-form @submit="onSubmit" ref="formRef">
      <van-field
        v-model="user.mobile"
        name="mobile"
        placeholder="请填写手机号"
        :rules="rules.mobile"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请填写验证码"
        :rules="rules.code"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down :time="60*1000" format="ss 秒" v-if="isCountDownShow" @finish="isCountDownShow = false"/>
          <van-button v-else class="send-sms-btn" native-type="button" type="default" size="mini" round @click="onSendCode">获取验证码</van-button>
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendSms } from '@/api/user'
export default {
  name: 'LoginPage',
  components: {},
  data () {
    return {
      user: {
        mobile: '18615710930',
        code: '246810'
      },
      rules: {
        mobile: [
          { required: true, message: '请填写手机号' },
          { pattern: /^1[3|4|5|6|7|8|9]\d{9}$/, message: '请填写正确的手机号' }
        ],

        code: [{ required: true, message: '请填写验证码' }, { pattern: /^\d{6}$/, message: '请填写正确的验证码' }]
      },
      isCountDownShow: false // 控制倒计时显示
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onSendCode () {
      // console.log('点击按钮')
      try {
        // 校验手机号是否正确
        // 通过ref得到组件实例
        await this.$refs.formRef.validate('mobile')
        console.log('校验成功')
        // 发送请求
        await sendSms(this.user.mobile)
        // 显示倒计时
        this.isCountDownShow = true
      } catch (e) {
        // 2种错误
        // 错误1-手机号不正确, e = {name: 'mobile', message: '请填写正确的手机号'}
        // 错误2-连续点击,e = {response: {status: 429}}
        if (e.response && e.response.status === 429) {
          this.$toast('请稍后重试')
        } else {
          this.$toast('手机号码不正确')
        }
      }
    },
    // 通过校验才会触发submit
    async onSubmit (values) {
      try {
        console.log('submit', values)
        const res = await login(values)
        this.$store.commit('setToken', res.data.data.token)
        this.$toast.success('登录成功')
        this.$router.push('/')
      } catch (e) {
        console.log(e)
        if (e.response.status === 400) {
          this.$toast.fail('登录失败')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .toutiao {
    font-size: 37px;
  }
    .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
</style>
